package com.bll.tsdoa.controller;


import com.alibaba.fastjson.JSON;
import com.bll.tsdoa.convert.RawConvertDtoToEntity;
import com.bll.tsdoa.dto.RawDto;
import com.bll.tsdoa.entity.Raw;
import com.bll.tsdoa.entity.Result;
import com.bll.tsdoa.service.RawService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 原料(Raw)表控制层
 *
 * @author makejava
 * @since 2025-02-20 00:05:46
 */
@RestController
@RequestMapping("/oneDimension/raw")
@Slf4j
public class RawController {
    @Resource
    private RawService rawService;

    /**
     * 新增原料到数据库
     *
     * @param rawDto
     *
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody RawDto rawDto) {

        if (log.isInfoEnabled()) {
            log.info("RawController.add.dto::{}", JSON.toJSONString(rawDto));
        }
        Preconditions.checkNotNull(rawDto.getId(), "原料id不能为空");
        Preconditions.checkNotNull(rawDto.getLength(), "原料长度不能为空");
        Raw raw = RawConvertDtoToEntity.INSTANCE
                .convertDtoToEntity(rawDto);
        boolean result = rawService.save(raw);
        if (result) {
            return Result.success();
        }
        return Result.error("插入失败");
    }

    /**
     * 删除原料
     *
     * @param rawDto
     *
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody RawDto rawDto) {
        try {

            if (log.isInfoEnabled()) {
                log.info("RawController.delete.dto::{}", JSON.toJSONString(rawDto));
            }
            Preconditions.checkNotNull(rawDto.getId(), "原料id不能为空");
            Preconditions.checkNotNull(rawDto.getLength(), "原料长度不能为空");
            Raw raw = RawConvertDtoToEntity.INSTANCE
                    .convertDtoToEntity(rawDto);
            boolean result = rawService.removeById(raw.getId());
            if (result) {
                return Result.success();
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 原料更新
     *
     * @param rawDto
     *
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody RawDto rawDto) {
        try {

            if (log.isInfoEnabled()) {
                log.info("RawController.update.dto::{}", JSON.toJSONString(rawDto));
            }
            Preconditions.checkNotNull(rawDto.getId(), "原料id不能为空");
            Preconditions.checkNotNull(rawDto.getLength(), "原料长度不能为空");
            Raw raw = RawConvertDtoToEntity.INSTANCE
                    .convertDtoToEntity(rawDto);
            boolean result = rawService.updateById(raw);
            if (result) {
                return Result.success();
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}

